package com.example.sqliteviewer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class DatabaseAdapter extends RecyclerView.Adapter<DatabaseAdapter.DatabaseViewHolder> {

    private final Context context;
    private final List<File> databaseFiles;
    private final OnDatabaseClickListener listener;
    private final SimpleDateFormat dateFormat;

    public interface OnDatabaseClickListener {
        void onDatabaseClick(File databaseFile);
    }

    public DatabaseAdapter(Context context, List<File> databaseFiles, OnDatabaseClickListener listener) {
        this.context = context;
        this.databaseFiles = databaseFiles;
        this.listener = listener;
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    }

    @NonNull
    @Override
    public DatabaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_database, parent, false);
        return new DatabaseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DatabaseViewHolder holder, int position) {
        File file = databaseFiles.get(position);
        
        holder.tvDatabaseName.setText(file.getName());
        holder.tvDatabasePath.setText(file.getAbsolutePath());
        
        // 显示文件大小
        long fileSize = file.length();
        String fileSizeStr;
        if (fileSize < 1024) {
            fileSizeStr = fileSize + " B";
        } else if (fileSize < 1024 * 1024) {
            fileSizeStr = String.format(Locale.getDefault(), "%.2f KB", fileSize / 1024.0);
        } else {
            fileSizeStr = String.format(Locale.getDefault(), "%.2f MB", fileSize / (1024.0 * 1024));
        }
        holder.tvDatabaseSize.setText(fileSizeStr);
        
        // 显示最后修改时间
        Date lastModified = new Date(file.lastModified());
        holder.tvDatabaseDate.setText(dateFormat.format(lastModified));
        
        holder.cardView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDatabaseClick(file);
            }
        });
    }

    @Override
    public int getItemCount() {
        return databaseFiles.size();
    }

    static class DatabaseViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        TextView tvDatabaseName;
        TextView tvDatabasePath;
        TextView tvDatabaseSize;
        TextView tvDatabaseDate;

        public DatabaseViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.cardView);
            tvDatabaseName = itemView.findViewById(R.id.tvDatabaseName);
            tvDatabasePath = itemView.findViewById(R.id.tvDatabasePath);
            tvDatabaseSize = itemView.findViewById(R.id.tvDatabaseSize);
            tvDatabaseDate = itemView.findViewById(R.id.tvDatabaseDate);
        }
    }
}